package ys.mihoyo.ymcc.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import ys.mihoyo.ymcc.domain.*;
import ys.mihoyo.ymcc.dto.CourseParamDto;
import ys.mihoyo.ymcc.mapper.CourseMapper;
import ys.mihoyo.ymcc.service.*;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collector;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author mihoyo
 * @since 2022-12-06
 */
@Service
public class CourseServiceImpl extends ServiceImpl<CourseMapper, Course> implements ICourseService {

    @Autowired
    private ICourseDetailService courseDetailService;
    @Autowired
    private ICourseMarketService courseMarketService;
    @Autowired
    private ICourseResourceService courseResourceService;
    @Autowired
    private ITeacherService teacherService;
    @Autowired
    private ICourseTeacherService courseTeacherService;
    @Autowired
    private ICourseSummaryService courseSummaryService;

    @Override
    public void save(CourseParamDto courseParamDto) {
        //保存课程数据
        Course course = courseParamDto.getCourse();
        course.setLoginId(2L);
        course.setLoginUserName("刻晴");
        course.setStatus(Course.OFFLINE);
        //讲师名称，因为有可能是多个所以需要分割
        List<Teacher> teachers = teacherService.selectBatchIds(courseParamDto.getTeacharIds());
        //获取集合中老师的姓名，并且进行拼接
        String names = teachers.stream().map(Teacher::getName).collect(Collectors.joining(","));
        course.setTeacherNames(names);
        insert(course);
        //设置课程详情
        CourseDetail courseDetail = courseParamDto.getCourseDetail();
        courseDetail.setId(course.getId());
        courseDetailService.insert(courseDetail);

        //设置营销数据
        CourseMarket courseMarket = courseParamDto.getCourseMarket();
        courseMarket.setId(course.getId());
        courseMarketService.insert(courseMarket);

        //设置资源数据
        CourseResource courseResource = courseParamDto.getCourseResource();
        courseResource.setCourseId(course.getId());
        courseResourceService.insert(courseResource);

        //设置初始化数据
        CourseSummary courseSummary = new CourseSummary();
        courseSummary.setId(course.getId());
        courseSummary.setCommentCount(0);
        courseSummary.setViewCount(0);
        courseSummary.setSaleCount(0);
        courseSummaryService.insert(courseSummary);

        //6.保存课程和讲师的中间表
//       for(Teacher teacher:teachers){
//           CourseTeacher courseTeacher = new CourseTeacher();
//           courseTeacher.setTeacherId(teacher.getId());
//           courseTeacher.setCourseId(course.getId());
//           courseTeacherService.insert(courseTeacher);
//       }
        CourseTeacher courseTeacher = new CourseTeacher();
        courseTeacher.setCourseId(course.getId());
        courseParamDto.getTeacharIds().forEach(tid->{
            courseTeacher.setTeacherId(tid);
            courseTeacherService.insert(courseTeacher);
        });
    }
}
